package hjslm.Controller.Background;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import hjslm.Bean.BaseTwoBean;
import hjslm.Bean.PictureBean;
import hjslm.Bean.TeacherBean;
import hjslm.Common.Impl.CommonFunImpl;
import hjslm.Service.IBaseTwoService;
import hjslm.Service.IPictureService;
import hjslm.Service.ITeacherService;
import framework.base.common.Pager;
import framework.base.controller.BaseController;

@Controller
@RequestMapping("/b_teacher")
public class TeacherController extends BaseController {

	/**
	 * @author fudum
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	ITeacherService teacherService;
	
	@Autowired
	IBaseTwoService baseTwoService;
	
	@Autowired
	IPictureService pictureService;

	@Override
	public void setLogger() {
		logger = LoggerFactory.getLogger(getClass());
	}

	@RequestMapping("/index")
	public ModelAndView index(Pager<TeacherBean> pager) {
		ModelAndView modelAndView = new ModelAndView("background/teacher/base/index");
		pager = teacherService.selectPage(null, pager);
		modelAndView.addObject("pager", pager);
		return modelAndView;
	}

	@RequestMapping("/add")
	public ModelAndView add() {
		ModelAndView modelAndView = new ModelAndView("background/teacher/base/add");
		BaseTwoBean baseTwoBean = new BaseTwoBean();
		//科目
		baseTwoBean.setBotype("科目");
		List<BaseTwoBean> listSubject = baseTwoService.selectList(baseTwoBean);
		modelAndView.addObject("listSubject", listSubject);
		//年级
		baseTwoBean.setBotype("年级");
		List<BaseTwoBean> listGrade = baseTwoService.selectList(baseTwoBean);
		modelAndView.addObject("listGrade", listGrade);
		//带班类型
		baseTwoBean.setBotype("地址");
		List<BaseTwoBean> listQuyu = baseTwoService.selectList(baseTwoBean);
		modelAndView.addObject("listQuyu", listQuyu);
		
		return modelAndView;
	}

	@RequestMapping("/insert")
	public void teacherInsert(TeacherBean bean,HttpServletResponse response) {
		bean.setId(CommonFunImpl.uuid("T"));//设置主键
		bean.setInserttime(CommonFunImpl.currentTime(1));//设置插入时间
		int retVal = teacherService.insert(bean);
		try {
			PrintWriter out = response.getWriter();
			out.print(retVal);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();// 打印异常
		}
	}

	@RequestMapping("/delete")
	public ModelAndView Delete(TeacherBean TeacherBean, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		int ret = teacherService.delete(TeacherBean);
		try {
			PrintWriter out = response.getWriter();
			out.print(ret);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/edit")
	public ModelAndView edit(TeacherBean bean,String tip) {
		ModelAndView modelAndView = new ModelAndView("background/teacher/base/edit");
		bean = teacherService.selectOne(bean);
		modelAndView.addObject("tea", bean);
		
		BaseTwoBean baseTwoBean = new BaseTwoBean();
		//科目
		baseTwoBean.setBotype("科目");
		List<BaseTwoBean> listSubject = baseTwoService.selectList(baseTwoBean);
		modelAndView.addObject("listSubject", listSubject);
		//年级
		baseTwoBean.setBotype("年级");
		List<BaseTwoBean> listGrade = baseTwoService.selectList(baseTwoBean);
		modelAndView.addObject("listGrade", listGrade);
		//地址
		baseTwoBean.setBotype("地址");
		List<BaseTwoBean> listQuyu = baseTwoService.selectList(baseTwoBean);
		modelAndView.addObject("listQuyu", listQuyu);
		
		PictureBean pictureBean = new PictureBean();
		pictureBean.setTeaid(bean.getId());
		List<PictureBean> listPicture = pictureService.selectList(pictureBean);
		modelAndView.addObject("listPicture", listPicture);
		
		return modelAndView;
	}
	
	// 班级插入 //3
	@RequestMapping("/update")
	public void update(TeacherBean bean,HttpServletResponse response) {
		bean.setInserttime(CommonFunImpl.currentTime(1));//设置插入时间
		int retVal = teacherService.update(bean);
		try {
			PrintWriter out = response.getWriter();
			out.print(retVal);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();// 打印异常
		}
	}
	
	@RequestMapping("/video")
	public ModelAndView video(String vid) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("background/teacher/video");
		modelAndView.addObject("vid", vid);
		return modelAndView;
	}
	
	
	/**
	 * 教师附加信息
	 * @return
	 */
	@RequestMapping("/teacheraddition")
	public ModelAndView teacherAddition() {
		ModelAndView modelAndView = new ModelAndView("background/teacher/addition/teacheraddition");
		Pager<TeacherBean> pager = new Pager<TeacherBean>();
		pager = teacherService.selectPage(null, pager);
		modelAndView.addObject("pager", pager);
		return modelAndView;
	}
	
}
